﻿layui.use(['layer', 'layedit', 'laydate'], function () {
    var layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;

    $(function () {

        init();
        $(document).pjax('a[data-pjax]', '#page-content', { timeout: 8000 });
        ///pjax开始（与服务器连接建立后触发）
        $(document).on('pjax:start', function () {
            //alert("开始");
        });
        ///pjax成功后触发
        $(document).on('pjax:success', function () {
            init();
        });
        ///pjax出错后触发
        $(document).on('pjax:error', function (e) {
            layer.msg("服务器繁忙，请稍后在试！", { icon: 2 });
            e.preventDefault();
        });
    })

})


function init() {
    $('.openWindow').unbind("click"); //移除click
    $(".openWindow").click(function (event) {
        event.preventDefault();
        var href = $(this).attr("href");
        var title = $(this).attr("title");
        var width = $(this).attr("width");
        var height = $(this).attr("height");
        if (width === undefined) {
            width = "50%;"
        }
        if (height === undefined) {
            height = "80%;"
        }
        layer.open({
            type: 2 //此处以iframe举例
            , title: title
            //, area: ['80%', '80%']
            , area: [width, height]
            , shade: 0.2
            , maxmin: true
            , content: href
            , btn: ['确定', '关闭']
            , yes: function (index, layero) {
                var body = layer.getChildFrame('body', index);
                var iframeWin = layero.find('iframe')[0]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                var form = body.find('form')[0];

                var loading = iframeWin.contentWindow.layer.load();//显示loading提示
                var options = {
                    success: function (result) {
                        //关闭loading提示
                        iframeWin.contentWindow.layer.close(loading);
                        if (result.success) {
                            swal({
                                title: result.message,
                                type: "success"
                            }, function () {
                                setTimeout(function () {
                                    //关闭Iframe
                                    layer.close(index);
                                    if (v && typeof (v.refresh) === "function") {
                                        v.refresh();
                                    }
                                }, 500);//延时0.1秒，对应360 7.1版本bug
                            });
                            //iframeWin.contentWindow.jmsg(result.message);
                            //刷新当前列表页数据
                            //$("#jqGrid").jqGrid("setGridParam").trigger("reloadGrid");  //重载JQGrid
                            //setTimeout(function () {
                            //    //关闭Iframe
                            //    layer.close(index);
                            //    if (typeof successCallback === 'function') {
                            //        successCallback.call(this);
                            //    }
                            //}, 1000);//延时0.1秒，对应360 7.1版本bug

                        } else {
                            swal({
                                title: result.message,
                                text: result.error,
                                type: "error"
                            });
                        }
                    }
                }
                $(form).ajaxSubmit(options);
            }
            , btn2: function () {
                layer.closeAll();
            }
        });
    });
}